//
//  JKSelectorPictureCell.swift
//  JKSelectorPicture
//
//  Created by 王冲 on 2018/1/28.
//  Copyright © 2018年 希爱欧科技有限公司. All rights reserved.
//

import UIKit

@objc
protocol JKSelectorPictureCellDelegate: NSObjectProtocol {
    
    @objc optional func photoDidAddSelector(cell:JKSelectorPictureCell)
    @objc optional func photoDidDeleteSelector(cell:JKSelectorPictureCell)
}

class JKSelectorPictureCell: UICollectionViewCell {
    
    weak var JKPhotoCellDelegate: JKSelectorPictureCellDelegate?
    
    var image: UIImage?
    {
        didSet{
            if image != nil{
                
                deleteBtn.isHidden = false
                addBtn.setImage(image!, for: UIControlState.normal)
                addBtn.isUserInteractionEnabled = false
            }else
            {
                deleteBtn.isHidden = true
                addBtn.isUserInteractionEnabled = true
                addBtn.setImage(UIImage(named: "compose_pic_add"), for: UIControlState.normal)
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUI()
    }
    
    func setUI() {
        
        contentView.backgroundColor = UIColor.purple
        
        // 1.添加添加和删除两个按钮
        contentView.addSubview(addBtn)
        contentView.addSubview(deleteBtn)
        
        // 2.布局子控件
        addBtn.translatesAutoresizingMaskIntoConstraints = false
        deleteBtn.translatesAutoresizingMaskIntoConstraints = false
        
        var cons = [NSLayoutConstraint]()
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[addBtn]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["addBtn" : addBtn])
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[addBtn]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["addBtn" : addBtn])
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[deleteBtn]-2-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["deleteBtn" : deleteBtn])
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:|-2-[deleteBtn]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["deleteBtn" : deleteBtn])
        contentView.addConstraints(cons)
    }
    
    // MARK: 添加的按钮
    private lazy var addBtn: UIButton = {
        
        let btn = UIButton()
        btn.addTarget(self, action: #selector(JKSelectorPictureCell.addClick), for: UIControlEvents.touchUpInside)
        btn.imageView?.contentMode = UIViewContentMode.scaleAspectFill
        btn.setImage(UIImage(named:"compose_pic_add"), for: UIControlState.normal)
        btn.setImage(UIImage(named:"compose_pic_add_highlighted"), for: UIControlState.highlighted)
        return btn
    }()
    
    // MARK: 删除的按钮
    private lazy var deleteBtn: UIButton = {
        
        let btn = UIButton()
        btn.isHidden = true
        btn .addTarget(self, action: #selector(JKSelectorPictureCell.deleteClick), for: UIControlEvents.touchUpInside)
        btn.setImage(UIImage(named:"compose_photo_close"), for: UIControlState.normal)
        return btn
    }()
    
    // MARK: 添加
    @objc func addClick() {
        
        print(#function)
        JKPhotoCellDelegate?.photoDidAddSelector!(cell: self)
    }
    
    // MARK: 删除
    @objc func deleteClick() {
        
        print(#function)
        JKPhotoCellDelegate?.photoDidDeleteSelector!(cell: self)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

